package controller

import (
	"context"
	"encoding/json"
	"image/png"
	"net/http"
	"web/api"
	"web/global"
	"web/proto/mycaptcha"

	"github.com/afocus/captcha"
	"github.com/gin-gonic/gin"
)

// 获取图片信息
func GetImageCd(ctx *gin.Context) {
	var req mycaptcha.Request
	var rsp mycaptcha.Response
	// 获取图片验证码 uuid
	req.Uuid = ctx.Param("uuid")
	c := api.NewCaptcha()

	err := c.Call(context.Background(), &req, &rsp)
	if err != nil {
		global.GlobalLog.Error(context.Background(), "获取图片信息错误:", err)
		resp := make(map[string]string)
		defer ctx.JSON(http.StatusOK, resp)
		//校验数据
		resp["errno"] = rsp.Errno
		resp["errmsg"] = rsp.Errmsg
		return
	}
	// 将得到的数据,反序列化,得到图片数据
	var img captcha.Image
	json.Unmarshal(rsp.Data, &img)

	// 将图片写出到 浏览器.
	png.Encode(ctx.Writer, img)

}
